class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def mergeInBetween(self, list1, a, b, list2):
        pre_mut_b = ListNode(0, list2)
        pre_b = pre_mut_b
        node = list2
        while node:
            pre_b = node
            node = node.next
        pre_mut_a = ListNode(0, list1)
        pre_a = pre_mut_a
        node = list1
        index = 0
        while node:
            if index == a:
                pre_a_1 = pre_a
            if index == b:
                node_a = node
            pre_a = node
            node = node.next
            index += 1
        pre_a_1.next = pre_mut_b.next
        pre_b.next = node_a.next
        return pre_mut_a.next
